package com.example.ccj.b.c.draw;

import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.u.ActivityUtils;
import com.github.gzuliyujiang.colorpicker.ColorPicker;
import com.github.gzuliyujiang.colorpicker.OnColorPickedListener;


/**
 * 涂鸦板
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/11 8:28
 */
public class DrawingBoardView extends RelativeLayout implements SignatureView.OnSignatureCompletedListener {
    ImageView mIvUndo,mIvRedo,mIvBackground,mIvColor,mIvClear,mIvImport,mIvExport,mIvEras,mIvBrush,mTvClose;
    SignatureView signatureView;//画板
    SeekBar seekBarWight;//画笔宽度
    LinearLayout linearLayout;

    public DrawingBoardView(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.linear_layout_drawing_boar,this);
        init();
    }

    public DrawingBoardView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.linear_layout_drawing_boar,this);
        init();
    }

    private void init(){
        mIvUndo = findViewById(R.id.drawing_boar_undo);
        mIvRedo = findViewById(R.id.drawing_boar_redo);
        mIvBackground = findViewById(R.id.drawing_boar_background);
        mIvColor = findViewById(R.id.drawing_boar_color_select);
        mIvClear = findViewById(R.id.drawing_boar_clear);
        mIvImport = findViewById(R.id.drawing_boar_import);
        mIvExport = findViewById(R.id.drawing_boar_export);
        signatureView = findViewById(R.id.drawing_boar_view);
        seekBarWight = findViewById(R.id.drawing_boar_seek_bar);
        mIvEras = findViewById(R.id.drawing_boar_erase);
        mIvBrush = findViewById(R.id.drawing_boar_brush);
        linearLayout = findViewById(R.id.drawing_boar_view_lin);
        mTvClose = findViewById(R.id.drawing_boar_close);

        signatureView.setOnSignatureCompletedListener(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            seekBarWight.setMin(1);
        }
        initOnclick();
    }

    private void initOnclick(){
        //撤销
        mIvUndo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.undo();
            }
        });

        //重做上一步
        mIvRedo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.redo();
            }
        });

        //更换背景色
        mIvBackground.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPicker colorPicker = new ColorPicker(ActivityUtils.getTopActivity());
                colorPicker.setOnColorPickListener(new OnColorPickedListener() {
                    @Override
                    public void onColorPicked(int pickedColor) {
                        linearLayout.setBackgroundColor(pickedColor);
                        signatureView.setSignatureBackgroundColor(pickedColor);
                    }
                });
                colorPicker.show();
            }
        });

        //更换画笔颜色
        mIvColor.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPicker colorPicker = new ColorPicker(ActivityUtils.getTopActivity());
                colorPicker.setOnColorPickListener(new OnColorPickedListener() {
                    @Override
                    public void onColorPicked(int pickedColor) {
                        signatureView.setColor(pickedColor);
                        // 设置滑块颜色
                        seekBarWight.setThumbTintList(ColorStateList.valueOf(pickedColor));
                        // 设置进度条颜色
                        seekBarWight.setProgressTintList(ColorStateList.valueOf(pickedColor));
                    }
                });
                colorPicker.show();
            }
        });

        //清除
        mIvClear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.clear();
            }
        });

        seekBarWight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                signatureView.setWidth(progress);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });

        mIvEras.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.toggleEraseMode();
                if (signatureView.isEraseMode()) {
                    mIvEras.setImageResource(R.mipmap.erase);
                    mIvBrush.setImageResource(R.mipmap.drawing_boar_brush_gray);
                } else {
                    mIvEras.setImageResource(R.mipmap.erase_gray);
                    mIvBrush.setImageResource(R.mipmap.drawing_boar_brush);
                }
            }
        });

        mIvBrush.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.toggleEraseMode();
                if (signatureView.isEraseMode()) {
                    mIvEras.setImageResource(R.mipmap.erase);
                    mIvBrush.setImageResource(R.mipmap.drawing_boar_brush_gray);
                } else {
                    mIvEras.setImageResource(R.mipmap.erase_gray);
                    mIvBrush.setImageResource(R.mipmap.drawing_boar_brush);
                }
            }
        });

        //导入
        mIvImport.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.loadSignatureFromFile("");
            }
        });

        //导出
        mIvExport.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                signatureView.saveSignatureToFile("");
            }
        });

        //关闭
        mTvClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setVisibility(GONE);
            }
        });
    }

    boolean isHaveRedo = true;
    @Override
    public void onUndo(int num) {
        if (isHaveRedo) {
            isHaveRedo = false;
            mIvRedo.setImageResource(R.mipmap.drawing_boar_redo);
        }
    }

    //重新绘制上一步撤销的动作  回撤
    @Override
    public void onRedo(int num) {
        if (num <= 0) {
            isHaveRedo = true;
            mIvRedo.setImageResource(R.mipmap.drawing_boar_redo_gray);
        }
    }
}

